package _算法思维系列._贪心算法之区间问题;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 吕庆龙
 * @Date: 2020/4/16 10:42
 * <p>
 * 功能描述:
 */
public class Summary_0986 {

    public static void main(String[] args) {
        Summary_0986 test = new Summary_0986();
        int[][] A = {{0,2},{5,10},{13,23},{24,25}};
        int[][] B = {{1,5},{8,12},{15,24},{25,26}};
        test.intervalIntersection(A,B);

    }

    /**
     * 输入：A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
     * 输出：[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
     */
    public int[][] intervalIntersection(int[][] A, int[][] B) {
        int i = 0, j = 0;
        List<int[]> res = new ArrayList<>();

        while (i < A.length && j < B.length) {
            int[] a = A[i];
            int[] b = B[j];

            if (a[1] >= b[0] && b[1] >= a[0]) {
                res.add(new int[]{Math.max(a[0],b[0]),Math.min(a[1],b[1])});
            }

            if (b[1] < a[1])
                j++;
            else
                i++;
        }
        return res.toArray(new int[0][]);
    }
}
